<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF预览</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            height: 100vh;
        }
        #toolbar {
            padding: 10px;
            background-color: #f8f9fa;
            border-bottom: 1px solid #ddd;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        #viewerContainer {
            flex: 1;
            overflow: auto;
            background-color: #525659;
            display: flex;
            justify-content: center;
        }
        #pdfViewer {
            background-color: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.3);
            margin: 20px;
        }
        .btn {
            padding: 5px 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: white;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #f0f0f0;
        }
        #pageNumber {
            width: 50px;
            text-align: center;
        }
        #zoomLevel {
            width: 70px;
        }
    </style>
</head>
<body>
    <div id="toolbar">
        <button class="btn" onclick="prevPage()">上一页</button>
        <input type="number" id="pageNumber" min="1" value="1">
        <span>/ <span id="pageCount">0</span></span>
        <button class="btn" onclick="nextPage()">下一页</button>
        <select id="zoomLevel" onchange="changeZoom()">
            <option value="0.5">50%</option>
            <option value="0.75">75%</option>
            <option value="1" selected>100%</option>
            <option value="1.25">125%</option>
            <option value="1.5">150%</option>
            <option value="2">200%</option>
        </select>
        <button class="btn" onclick="window.close()">关闭</button>
    </div>
    <div id="viewerContainer">
        <canvas id="pdfViewer"></canvas>
    </div>

    <script>
        // 配置PDF.js worker
        pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';

        let pdfDoc = null;
        let pageNum = 1;
        let scale = 1;
        const canvas = document.getElementById('pdfViewer');
        const ctx = canvas.getContext('2d');

        // 从URL获取PDF文件名
        const pdfUrl = '/preview/' + decodeURIComponent(window.location.pathname.split('/').pop());

        // 加载PDF
        pdfjsLib.getDocument(pdfUrl).promise.then(function(pdf) {
            pdfDoc = pdf;
            document.getElementById('pageCount').textContent = pdf.numPages;
            renderPage(pageNum);
        });

        // 渲染页面
        function renderPage(num) {
            pdfDoc.getPage(num).then(function(page) {
                const viewport = page.getViewport({scale: scale});
                canvas.height = viewport.height;
                canvas.width = viewport.width;

                const renderContext = {
                    canvasContext: ctx,
                    viewport: viewport
                };

                page.render(renderContext);
            });
        }

        // 上一页
        function prevPage() {
            if (pageNum <= 1) return;
            pageNum--;
            document.getElementById('pageNumber').value = pageNum;
            renderPage(pageNum);
        }

        // 下一页
        function nextPage() {
            if (pageNum >= pdfDoc.numPages) return;
            pageNum++;
            document.getElementById('pageNumber').value = pageNum;
            renderPage(pageNum);
        }

        // 页码输入
        document.getElementById('pageNumber').addEventListener('change', function() {
            const newPage = parseInt(this.value);
            if (newPage > 0 && newPage <= pdfDoc.numPages) {
                pageNum = newPage;
                renderPage(pageNum);
            } else {
                this.value = pageNum;
            }
        });

        // 缩放
        function changeZoom() {
            scale = parseFloat(document.getElementById('zoomLevel').value);
            renderPage(pageNum);
        }
    </script>
</body>
</html>
